#include "listwidgetitem.h"
#include "chatwidget.h"

#include <QDebug>

#include <QBrush>
#include <QPainter>
#include <QPen>
#include <QColor>
#include <QFontDatabase>
#include <QStringList>

const QString ListWidgetItem::m_defaultPhotoPath(":/images/icon/Chat.png");

ListWidgetItem::ListWidgetItem(const QString &uuid, QListWidget *parent)
    :QListWidgetItem(parent), m_uuid(uuid), m_noReaadMessageCount(0)
{
    //使用默认头像
    this->setPhoto(QIcon(m_defaultPhotoPath));
}

ListWidgetItem::~ListWidgetItem()
{
}

void ListWidgetItem::setName(const QString &name)
{
    this->QListWidgetItem::setText(name);
}

QString ListWidgetItem::name()
{
    return this->QListWidgetItem::text();
}

void ListWidgetItem::setPhoto(const QIcon &icon)
{
    QListWidgetItem::setIcon(icon);
    m_photo = icon;
    if(m_noReaadMessageCount > 0){
        this->drawNoReadMessageCount();
    }
}

QIcon ListWidgetItem::photo()
{
    return m_photo;
}

QString ListWidgetItem::uuid()
{
    return m_uuid;
}

void ListWidgetItem::drawNoReadMessageCount()
{
    //取出头像图片
    QPixmap pixmap = m_photo.pixmap(80, 80);
    QPainter painter(&pixmap);
    //在头像图片右下角画圆角红色长方形
    QPen pen(QColor(Qt::red));
    painter.setPen(pen);
    QBrush brush(QColor(Qt::red));
    painter.setBrush(brush);

    QRect rect(50, 60, 30, 20);
    painter.drawRoundedRect(rect, 5, 5);
    //在矩形中画消息数
    QFont font = painter.font();
    font.setPixelSize(19);
    font.setBold(true);
    painter.setFont(font);
    pen.setColor(Qt::white);
    painter.setPen(pen);
    //消息数量大于99则显示？？
    if(m_noReaadMessageCount <= 99){
        painter.drawText(rect, Qt::AlignHCenter, QString("%1").arg(m_noReaadMessageCount));
    }
    else{
        painter.drawText(rect, Qt::AlignHCenter, "??");
    }
    QListWidgetItem::setIcon(QIcon(pixmap));
}


/********************改变状态***************************/
void ListWidgetItem::changeStatusOnDoubleCliked()
{
    m_noReaadMessageCount = 0;
    QListWidgetItem::setIcon(m_photo);
    qDebug() << "changeStatusOnDoubleCliked()";
}

void ListWidgetItem::changeStatusOnHasNewMessage()
{
    ++m_noReaadMessageCount;
    this->drawNoReadMessageCount();
    qDebug() << "changeStatusOnHasNewMessage()";
}

void ListWidgetItem::changeStatusOnExit()
{
    this->setPhoto(QIcon(m_defaultPhotoPath));
    if(m_noReaadMessageCount > 0){
        this->drawNoReadMessageCount();
    }
    qDebug() << "changeStatusOnExit()";
}

/******************************************************/


